import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "../store";

Vue.use(VueRouter)

const routes = [
  {
    path:"/",
    redirect:"/login"
  },

  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/Index.vue'),
    children:[
      {
        path: '/home',
        name: 'Home',
        component: () => import('../views/Home.vue')
      },
      {
        path: '/about',
        name: 'About',
        component: () => import('../views/About.vue')
      },
      {
        path: '/supplier',
        name: 'supplier',
        component: () => import('../views/Supplier.vue')
      },
      {
        path: '/commodity',
        name: 'commodity',
        component: () => import('../views/Commodity.vue')
      },
      {
        path: '/staff',
        name: 'staff',
        component: () => import('../views/Staff.vue')
      },
    ]
  },
]

const router = new VueRouter({
  routes
})
// 全局守卫
router.beforeEach((to, from, next) => {
  if (store.state.user) {
      next();
  } else {
      if (to.path == "/login") {
          next();
      } else {
          next("/login");
      }
  }
});
// 防止路由跳转报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
export default router
